<?php
namespace app\index\controller;
// header('content-type:application/json');

use think\Controller;
use think\Db;

class Info extends controller
{
    public function index()
    {
        return $this->fetch();
    }

    public function schoolInfo()
    {
        $sql = Db::name("school")->getLastSql();
        $rs = Db::name("school")->select();
        return json_encode($rs);
    }

    public function getGoodsList()
    {
        $tid = input('tid');
        if ($tid == 0) {
            $rs = Db::name('goods')->order('gId', 'desc')
            ->select();
            return json_encode($rs);
        } else {
            $rs = Db::name('goods')->where('typeId',$tid)
            ->order('gId','desc')->select();
            return json_encode($rs);
         }
    }

    public function types() {
        // $rs = Db::name('types')->field('tName')->select();
        $rs = Db::name('types')->select();
        array_unshift($rs,[
                            'tId'=>0,
                            'tName'=>"综合"]);
        // dump($rs);
        return json_encode($rs);
    }

    public function search() {
        // TODO: 正则表达式判断key是否合法
        $key = input('key');
        $rs = Db::name('goods')->whereLike("gTitle|tags",'%'.$key.'%')
        ->select();
        return json_encode($rs);
    }

    public function getComments() {
        $gid = input('gid');
        $rs = Db::name('comments c')->where('goodId',$gid)
        ->where('cstatus',0)
        ->join('user u','c.uId=u.userId')
        ->field('cId,cContent,goodId,u.name,cstatus')
        ->select();
        return json_encode($rs);
    }

    public function getGoods() {
        $gid = input('gid');
        $rs = Db::name('goods g')->join('user u','u.userId=g.uid')
        ->where('gId',$gid)
        ->where('gstatus',0)
        ->field('gId,gTitle,gPic,gDetail,typeId,tags,price,vDate,reMark,g.uid,name')
        ->find();
        return json_encode($rs);
    }
}
